/**
 * 试管架store
 */
import { makeAutoObservable } from "mobx";
import { ITBadResp, ITShelf, ITShelfPlane } from "./interfaces";
import {
  fetchAddShelf,
  fetchAllShelf,
  fetchDelShelf,
  fetchUpdateShelf,
} from "../api/shelf";

import { ITResponseData, checkResonse } from "../api";

export interface ITShelfStore {
  // 显示新增编辑弹窗
  showAddEditShelfModal: boolean;

  // 修改显示新增编辑弹窗
  changeShowAddEditShelfModal: (data: boolean) => void;

  // 当前编辑的试剂架数据
  editShelfData: ITShelf | null;

  // 修改当前正在编辑的试管架
  changeEditShelfData: (data: any) => void;

  // 当前试管架示例数据
  currentShelfPlane: ITShelfPlane;

  // 清除当前示例数据
  clearCurrentShelfPlane: () => void;

  // 修改当前试管架示例数据
  changeCurrentShelfPlane: (data: any) => void;

  // 新增试管架loading
  addShelfLoading: boolean;

  // 修改新增试管架loading
  changeAddShelfLoading: (data: boolean) => void;

  // 新增试管架
  addShelf: (data: any) => Promise<ITBadResp | null>;

  // 更新试管架
  updateShelf: (data: any) => Promise<ITBadResp | null>;

  // 所有试管架
  allShelf: ITShelf[];

  // 修改所有试管架
  changeAllShelf: (data: ITShelf[]) => void;

  // 获取所有试管架请求状态
  allShelfLoading: boolean;

  // 修改获取所有试管架请求状态
  changeAllShelfLoading: (data: boolean) => void;

  // 获取所有试管架
  getAllShelf: () => Promise<ITBadResp | void>;

  // 删除试管架请求状态
  delShelfLoading: boolean;

  // 修改删除试管架请求状态
  changeDelShelfLoading: (data: boolean) => void;

  // 删除试管架
  delShelf: (data: any) => Promise<ITBadResp | null>;


}

class ShelfStore {
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true });
  }

  // 显示新增编辑弹窗
  showAddEditShelfModal = false;

  // 修改显示新增编辑弹窗
  changeShowAddEditShelfModal = (data: boolean) =>
    (this.showAddEditShelfModal = data);

  // 当前编辑的试剂架数据
  editShelfData: ITShelf | null = null;

  // 修改当前正在编辑的试管架
  changeEditShelfData = (data: any) => (this.editShelfData = data);

  // 当前试管架示例数据
  currentShelfPlane: ITShelfPlane = {};

  // 清除当前示例数据
  clearCurrentShelfPlane = () => (this.currentShelfPlane = {});

  // 修改当前试管架示例数据
  changeCurrentShelfPlane = (data: any) => {
    this.currentShelfPlane = {
      ...this.currentShelfPlane,
      ...data,
    };
  };

  // 新增试管架loading
  addShelfLoading: boolean = false;

  // 修改新增试管架loading
  changeAddShelfLoading = (data: boolean) => (this.addShelfLoading = data);

  // 所有试管架
  allShelf: ITShelf[] = [];

  // 修改所有试管架
  changeAllShelf = (data: ITShelf[]) => (this.allShelf = data);

  // 新增试管架
  addShelf = async (data: any): Promise<ITBadResp | null> => {
    this.changeAddShelfLoading(true);
    const res = await fetchAddShelf(data);
    this.changeAddShelfLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 更新试管架
  updateShelf = async (data: any): Promise<ITBadResp | null> => {
    this.changeAddShelfLoading(true);
    const res = await fetchUpdateShelf(data);
    this.changeAddShelfLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 获取所有试管架请求状态
  allShelfLoading = false;

  // 修改获取所有试管架请求状态
  changeAllShelfLoading = (data: boolean) => (this.allShelfLoading = data);

  // 获取所有试管架
  getAllShelf = async (): Promise<ITBadResp | void> => {
    this.changeAllShelfLoading(true);
    const res = await fetchAllShelf();
    this.changeAllShelfLoading(false);
    if (checkResonse(res)) {
      const result: ITResponseData = res.data;
      const { data } = result;
      data.forEach((item: ITShelf, index: number) => (item.key = index));
      this.changeAllShelf(data);
      return data;
    }
  };

  // 删除试管架请求状态
  delShelfLoading = false;

  // 修改删除试管架请求状态
  changeDelShelfLoading = (data: boolean) => (this.delShelfLoading = data);

  // 删除试管架
  delShelf = async (data: any): Promise<ITBadResp | null> => {
    this.changeDelShelfLoading(true);
    const res = await fetchDelShelf(data);
    this.changeDelShelfLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };


}

const shelfStore: ITShelfStore = new ShelfStore();
export default shelfStore;
